package com.db;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.test.Student;

public class Mymongodb {
	static private String dbpath = "";
	static private Element dbsroot;
	static private Document doc;
	static private final String config = "config.xml";
	static private File configfile = new File(dbpath + config);
	static private File nowdbfile;
	private String nowdb = "test";
	static private Document dbdoc;

	/**
	 * @return the dbpath
	 */
	public static String getDbpath() {
		return dbpath;
	}

	/**
	 * @param dbpath
	 *            the dbpath to set
	 */
	public static void setDbpath(String dbpath) {
		Mymongodb.dbpath = dbpath;
	}

	/**
	 * @return the nowdb
	 */

	/**
	 * @param dbname
	 * 
	 */

	private void init() {
		try {
			Document doc = new Document();
			Element dbsroot = new Element("databases");
			Element testdb = new Element("database");
			testdb.setAttribute("name", "test");
			dbsroot.addContent(testdb);
			doc.setRootElement(dbsroot);
			XMLOutputter out = new XMLOutputter();
			out.output(doc, new FileWriter(configfile));
			System.out.println("-------------初始化数据库成功----------------");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public Mymongodb() {

	}

	public Mymongodb(String dbname) {
		// init();
		createDb(dbname);
	}

	public void createDb(String dbname) {
		XMLOutputter out = new XMLOutputter();
		String location = dbpath + dbname + ".xml";
		nowdbfile = new File(location);
		/**
		 * 创建数据
		 * 
		 * 
		 */
		SAXBuilder saxb = new SAXBuilder();
		try {
			doc = saxb.build(configfile);
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		dbsroot = doc.getRootElement();
		List<Element> dbs = dbsroot.getChildren();
		for (Element db : dbs) {
			if (db.getAttributeValue("name").equals(dbname)) {
				return;
			}
		}
		// 不存在则添加
		Element dbE = new Element("database");
		dbE.setAttribute("name", dbname);
		Element locationE = new Element("location");
		locationE.addContent(location);
		dbE.addContent(locationE);
		dbsroot.addContent(dbE);
		try {
			out.output(doc, new FileWriter(configfile));
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		/**
		 * 新建数据库文件
		 */
		try {

			Document doc = new Document();
			Element root = new Element("database");
			doc.setRootElement(root);
			out.output(doc, new FileWriter(nowdbfile));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		nowdb = dbname;
	}

	public void dropdb(String dbname) {
		Element root = doc.getRootElement();
		List<Element> dbs = root.getChildren();
		for (Element db : dbs) {
			if (db.getAttributeValue("name").equals(dbname)) {
				dbs.remove(db);
			}
		}
		XMLOutputter out = new XMLOutputter();
		try {
			out.output(doc, new FileWriter(configfile));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @return the nowdb
	 */
	public String getNowdb() {
		return nowdb;
	}

	/**
	 * @param nowdb
	 *            the nowdb to set
	 */
	public void setNowdb(String nowdb) {
		this.nowdb = nowdb;
	}

	public void createCollection(String collName) {
		SAXBuilder saxb = new SAXBuilder();
		try {
			dbdoc = saxb.build(nowdbfile);
			Element root = dbdoc.getRootElement();
			List<Element> colls = root.getChildren();
			for (Element e : colls) {
				if (e.getAttributeValue("name").equals(collName)) {
					return;
				}
			}
			Element newcoll = new Element("collection");
			newcoll.setAttribute("name", collName);
			root.addContent(newcoll);
			XMLOutputter out = new XMLOutputter();
			out.output(dbdoc, new FileWriter(nowdbfile));
			System.out.println("-------------创建集合成功" + collName
					+ "----------------");
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public void insertdata(String collName, Object po) {
		SAXBuilder saxb = new SAXBuilder();
		Gson gson = new Gson();
		/**
		 * 插入数据
		 */
		Element data = new Element("data");
		Map<String, Object> map = new HashMap<String, Object>();
		String _id = UUID.randomUUID().toString();
		map.put("_id", UUID.randomUUID().toString());
		System.out.println(gson.fromJson(gson.toJson(po), Map.class));
		map.putAll(gson.fromJson(gson.toJson(po), Map.class));
		data.addContent(gson.toJson(map));

		try {
			dbdoc = saxb.build(nowdbfile);
			Element root = dbdoc.getRootElement();
			List<Element> colls = root.getChildren();
			for (Element x : colls) {
				if (x.getAttributeValue("name").equals(collName)) {
					x.addContent(data);
				}
			}

			XMLOutputter out = new XMLOutputter();
			out.output(dbdoc, new FileWriter(nowdbfile));
			System.out.println("-------------插入数据功----------------");
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public List listdata(String collName) {
		SAXBuilder saxb = new SAXBuilder();
		/**
		 * 查询数据
		 */

		List<String> datalist = new ArrayList();
		try {
			dbdoc = saxb.build(nowdbfile);
			Element root = dbdoc.getRootElement();
			List<Element> colls = root.getChildren();
			for (Element x : colls) {
				if (x.getAttributeValue("name").equals(collName)) {
					List<Element> datalist2 = x.getChildren();
					for (Element data : datalist2) {
						datalist.add(data.getText());
					}
				}
			}

		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return datalist;

	}

	public String selectdata(String collName, Object po) {
		SAXBuilder saxb = new SAXBuilder();
		Gson gs = new Gson();
		Map map;
		try {
			dbdoc = saxb.build(nowdbfile);
			Element root = dbdoc.getRootElement();
			List<Element> colls = root.getChildren();
			for (Element e : colls) {
				if (e.getAttributeValue("name").equals(collName)) {
					List<Element> datas = e.getChildren();
					for (Element dataE : datas) {
						map = gs.fromJson(dataE.getText(), Map.class);
						Map newmap = gs.fromJson(gs.toJson(po), Map.class);
						if (map.get("_id").equals(newmap.get("_id")))
							return dataE.getText();
						break;
					}
				}
			}
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void deletedata(String collName, Object po) {
		SAXBuilder saxb = new SAXBuilder();
		Gson gs = new Gson();

		try {
			dbdoc = saxb.build(nowdbfile);
			Element root = dbdoc.getRootElement();
			List<Element> colls = root.getChildren();
			for (Element e : colls) {
				if (e.getAttributeValue("name").equals(collName)) {
					List<Element> datas = e.getChildren();
					for (Element dataE : datas) {
						Map map = gs.fromJson(dataE.getText(), Map.class);
						Map newmap = gs.fromJson(gs.toJson(po), Map.class);
						if (map.get("_id").equals(newmap.get("_id")))
							e.removeContent(dataE);
						break;
					}
				}
			}

			XMLOutputter out = new XMLOutputter();
			out.output(dbdoc, new FileWriter(nowdbfile));
			System.out.println("------------删除成功----------------");
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void updatedata(String collName, Object po) {
		SAXBuilder saxb = new SAXBuilder();
		Gson gs = new Gson();

		try {
			dbdoc = saxb.build(nowdbfile);
			Element root = dbdoc.getRootElement();
			List<Element> colls = root.getChildren();
			for (Element e : colls) {
				if (e.getAttributeValue("name").equals(collName)) {
					List<Element> datas = e.getChildren();
					for (Element dataE : datas) {
						Map map = gs.fromJson(dataE.getText(), Map.class);
						Map newmap = gs.fromJson(gs.toJson(po), Map.class);
						if (map.get("_id").equals(newmap.get("_id")))
							dataE.setText(gs.toJson(newmap));
						break;
					}
				}
			}

			XMLOutputter out = new XMLOutputter();
			out.output(dbdoc, new FileWriter(nowdbfile));
			System.out.println("------------更新成功----------------");
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
